<?php

namespace App\Wechat\Remind\Middleware;

use App\Exceptions\ApiException;
use Closure;

class Login
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
    	/**未提供token，禁止访问**/
    	if (!auth()->guard('remind')->getToken()){
    		throw new ApiException('Token not provided', 401);
	    }
	    /**检测token有效性**/
		if (!auth()->guard('remind')->check()){
			throw new ApiException('Token has expired', 401);
		}
	    /**不能解析到用户**/
	    if (!auth()->guard('remind')->user()){
		    throw new ApiException('User not found', 401);
	    }

        return $next($request);
    }
}
